{% extends "master.html" %}
{% block subtitle %} - 主面板 - 仓位管理{% endblock %}

{% block extJS %}
<script language="JavaScript" type="text/javascript">
//<![CDATA[
    function toSbumit(){
        $(".searchForm").attr('action',"{{url_for('.view',action='index')}}")
        $(".searchForm").submit();
    }
    
    function toExport(){
        $(".searchForm").attr('action',"{{url_for('.view',action='export')}}")
        $(".searchForm").submit();
    }
    
    function toUpdate(){
        clearMsg();
        var msg = [];
        if($("input[name='pdt_ivt']:checked").length < 1){
            msg.push('请选择至少一条记录以进行该操作！');
        }
        if(msg.length > 0 ){
            alertError(msg);
            return false;            
        }else{
            $("#dataForm").attr('action',"{{url_for('.view',action='updateQty')}}");
            $("#dataForm").submit();
        }
    }
    
    
    function toInternal(){
        clearMsg();
        var msg = [];
        if($("input[name='pdt_ivt']:checked").length < 1){
            msg.push('请选择至少一条记录以进行该操作！');
        }
        if(msg.length > 0 ){
            alertError(msg);
            return false;            
        }else{
            $("#dataForm").attr('action',"{{url_for('.view',action='internalCreate')}}");
            $("#dataForm").submit();
        }
    }
    
    function showItems(pdtID,ivtID){
        var params = {
            'pdtID' : pdtID,
            'ivtID' : ivtID,
            't' : nowstr()
        }
        $.getJSON("{{url_for('bpIvt.view',action='ajaxSrhItems')}}",params,function(r){
            if(r.code != 0 ){
                alert(r.msg);
            }else{
                var html = '';
                for(var i=0;i<r.data.length;i++){
                    html += '<div class="span4">' + r.data[i] + '</div>';
                }
                $("#itemList").html(html);
                $("#pdtstrong").text(r.pdt);
                $("#ivtstrong").text(r.ivt);
                $("#itemModal").modal('show');       
            }
        })
    }   

//]]>
</script>
{% endblock %}

{% block main %}
<div class="container">
    <div class="row">
        <ul class="breadcrumb">
          <li><a href="{{url_for('bpRoot.view')}}">主面板</a> <span class="divider">\</span></li>
          <li class="active">仓位管理</li>
        </ul>
    </div>
    <div class="row">
         {{mymacros.render_search_form(form,3)}}
    </div>
    <div class="row">
        <div class="btn-toolbar pull-right">
            <div class="btn-group"><button type="button" class="btn" onclick="return toSbumit()">{{BTN_SEARCH}}</button></div>
            <div class="btn-group"><a href="{{url_for('.view',action='add')}}" class="btn btn-primary">创建仓位</a></div>
            <!-- 
            <div class="btn-group"><button type="button" class="btn btn-primary" onclick="return toUpdate()">仓位调整</button></div>
            -->
            <div class="btn-group"><button type="button" class="btn btn-primary" onclick="return toInternal()">内部转仓</button></div>
            <div class="btn-group"><button type="button" class="btn btn-success" onclick="return toExport()">导出记录</button></div>
        </div>
    </div>
    <div class="row">
        <form method='post' action="{{ url_for('bpIvt.view') }}" enctype="multipart/form-data" id="dataForm">
        <table class="table table-bordered">
            <thead>
                <tr>
                    
                    <td class="span3">商品编号</td>
                    <td class="span2">商品名称</td>
                    <td><input type="checkbox" onclick="selectall(this,'pdt_ivt')"/>&nbsp;所处仓位</td>
                    <td class="span2">现存/可用数量</td>
                    <td class="span2">现存总数/可用总数</td>
                </tr>
            </thead>
            <tbody>

                
                
                {% for r in result %}
                    {% set pdt = r['product'] %}
                    <tr>
                        <td rowspan="{{r['data']|length}}">{{mymacros.render_goto_popup_pdt(pdt, loop.first)}}</td>
                        <td rowspan="{{r['data']|length}}">{{pdt.name}}</td>
                    {% for (ivt,ip) in r['data'] %}
                        {% if not loop.first %}
                            <tr>
                        {% endif %}                        
                            <td>
                                <input type="checkbox" name="pdt_ivt" value="{{pdt.id}}_{{ivt.id}}"/>&nbsp;
                                
                                <div class="btn-group">
                                  <a class="btn  btn-mini" onclick="showItems({{pdt.id}},{{ivt.id}})" href="#">{{ivt}}</a>
                                  <button class="btn dropdown-toggle  btn-mini" data-toggle="dropdown">
                                    <span class="caret"></span>
                                  </button>
                                  <ul class="dropdown-menu">
                                    <li><a href="{{url_for('.view',action='update',id=ivt.id)}}"><i class="icon-edit"></i>&nbsp;更新仓位信息</a></li>
                                    <li><a href="#" onclick="showItems({{pdt.id}},{{ivt.id}})"><i class="icon-list"></i>&nbsp;该商品在本仓位的个体列表</li>
                                  </ul>
                                </div>
                            </td>                                
                            <td>{{ip.qty}}/{{ip.availableQty}}</td>
                        {% if loop.first %}
                            <td rowspan="{{r['data']|length}}">{{r['totalQty']}}/{{r['totalAvailableQty']}}</td>
                        {% endif %}         
                        </tr>
                    {% endfor %}
                {% endfor %}
            </tbody>                
        </table>
        </form>
        {{mymacros.mypaginate(result)}}
        <p><sub>*</sub>可用数量 = 现存数量 - 被预留或将要出库的数量</p>
    </div>
</div>
    

<!-- modal begin -->
<div id="itemModal" class="modal hide fade" style="display: none;">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    商品&nbsp;[<strong id="pdtstrong"></strong>]&nbsp;在仓位&nbsp;[<strong id="ivtstrong"></strong>]&nbsp;中的个体列表
  </div>
  <div class="modal-body" >
    <div class="row-fluid">
        <div class="span12" id="itemList"></div>
    </div>
  </div>
  <div class="modal-footer">
    <button type="button" data-dismiss="modal" class="btn btn-primary">{{BTN_CLOSE}}</button>
  </div>
</div>
<!-- modal end -->
 
{% endblock %}
